package qqserver.service;

import qqcommon.Message;
import qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class ServerConnectClientThread extends Thread {
    private Socket socket;
    private String userId;//连接服务端的用户id
    private ArrayList<Message> arrayList = new ArrayList<>();

    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {

        //如果离线消息集合中有此用户的消息
        if (OfflineMessageService.getArrayList(userId) != null) {
            try {
                //取出当前用户对应的存储消息的集合，遍历输出给客户端
                ArrayList<Message> arrayList = OfflineMessageService.getArrayList(userId);

                for (Message message1 : arrayList) {
                    ObjectOutputStream oos =
                            new ObjectOutputStream(ManageServerConnectClientThread
                                    .getServerConnectClientThread(userId)
                                    .getSocket()
                                    .getOutputStream());
                    oos.writeObject(message1);
                }
                //删除对应集合
                OfflineMessageService.removeArrayList(userId);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        while (true) {
            try {
                System.out.println("服务端和客户端" + userId + "保持通信...");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();

                if (message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)) {//如果读取到客户端请求在线用户列表的信息

                    System.out.println(message.getSender() + "请求在线用户列表...");
                    //得到当前在线的用户
                    String s = ManageServerConnectClientThread.getKey();
                    Message retmessage = new Message();
                    retmessage.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                    retmessage.setContent(s);
                    retmessage.setReceiver(message.getSender());
                    //输出给发送者
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(retmessage);
                } else if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES)) {//如果读取到私聊信息
                    if (ManageServerConnectClientThread
                            .getServerConnectClientThread(message.getReceiver()) != null) {//如果接受者在线
                        //获取接受者的线程和socket
                        ServerConnectClientThread scct =
                                ManageServerConnectClientThread
                                        .getServerConnectClientThread(message.getReceiver());

                        Socket receiverSocket = scct.getSocket();
                        //通过接收者的服务端向其客户端发送信息
                        ObjectOutputStream oos = new ObjectOutputStream(receiverSocket.getOutputStream());
                        oos.writeObject(message);
                    } else {//如果接收者不在线
                        if (OfflineMessageService.getArrayList(message.getReceiver()) == null) {//如果集合中没有当前离线接收者ArrayList的集合
                            ArrayList<Message> arrayList = new ArrayList<>();
                            OfflineMessageService.addArrayList(message.getReceiver(), arrayList);
                        }
                        //得到离线用户的信息存储集合，存储离线信息
                        ArrayList<Message> receiverArrayList = OfflineMessageService.getArrayList(message.getReceiver());
                        receiverArrayList.add(message);

                    }

                } else if (message.getMesType().equals(MessageType.MESSAGE_MASS_MES)) {//如果是群发消息

                    HashMap<String, ServerConnectClientThread> hm = ManageServerConnectClientThread.getHm();
                    Set<String> keySet = hm.keySet();
                    //遍历除发送者之外的在线用户
                    for (String key : keySet) {
                        if (!(key.equals(message.getSender()))) {//如果不是发送者
                            ServerConnectClientThread scct = hm.get(key);
                            Socket receiverSocket = scct.getSocket();
                            //通过接收者的服务端向其客户端发送信息
                            ObjectOutputStream oos = new ObjectOutputStream(receiverSocket.getOutputStream());
                            oos.writeObject(message);
                        }
                    }


                } else if (message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)) {//如果读取到客户端请求退出系统
                    System.out.println(message.getSender() + "请求退出...");
                    //从线程集合中删除对应的线程
                    ManageServerConnectClientThread.removeServerConnectClientThread(message.getSender());
                    socket.close();
                    //退出线程
                    break;

                } else if (message.getMesType().equals(MessageType.MESSAGE_FILE_MES)) {//如果读取到请求传输文件
                    if(ManageServerConnectClientThread
                            .getServerConnectClientThread(message
                                    .getReceiver()) != null){
                        //根据接受者的id获取到对应的线程
                        ServerConnectClientThread scct = ManageServerConnectClientThread
                                .getServerConnectClientThread(message
                                        .getReceiver());
                        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(message);
                    } else {//如果接收者不在线
                        if (OfflineMessageService.getArrayList(message.getReceiver()) == null) {//如果集合中没有当前离线接收者ArrayList的集合
                            ArrayList<Message> arrayList = new ArrayList<>();
                            OfflineMessageService.addArrayList(message.getReceiver(), arrayList);
                        }
                        //得到离线用户的信息存储集合，存储离线信息
                        ArrayList<Message> receiverArrayList = OfflineMessageService.getArrayList(message.getReceiver());
                        receiverArrayList.add(message);
                    }

                } else {
                    System.out.println("暂未开放...");
                }


            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
